package alibobo.util.timer;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.Timer;

public abstract class absTimer {

	private int m_interval = 1000;
	private Timer m_timer;

	boolean m_enabled = false;

	public boolean isEnabled() {
		return m_enabled;
	}

	public abstract void callBack();

	protected void createTimer() {
		m_timer = new Timer(m_interval, new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				callBack();
				if (m_enabled == false) {
					m_timer.stop();
				}
			}
		});
		m_timer.setInitialDelay(m_interval);
		m_timer.setRepeats(true);

	}

	public void setEnabled(boolean mEnabled) {
		if (mEnabled == false) {
			m_enabled = false;
			if (m_timer != null)
				m_timer.stop();

		} else {
			if (m_enabled != true) {
				if (m_timer == null) {
					createTimer();
				}
				m_enabled = true;
				m_timer.start();
			}
		}

	}

	public int getInterval() {
		return m_interval;
	}

	public void setInterval(int mInterval) {
		m_interval = mInterval;
		if (m_timer != null) {
			m_timer.setDelay(m_interval);
		}
	}

}
